﻿using Nest;

namespace elasticsearchDemo.Model
{

    public class Product//:ElasticsearchEntity
    {
        /// <summary>
        /// id
        /// </summary>
  
        public long id { get; set; }
        public string Name { get; set; }
        /// <summary>
        /// 创建时间
        /// </summary>
        [Date(Name = "create_datetime")]
        public DateTimeOffset CreateDateTime { get; set; }
        private long? _timestamp;


        [Number(NumberType.Long, Name = "timestamp")]
        public long Timestamp
        {
            get
            {
                _timestamp ??= GetTimeStamp();
                return _timestamp.Value;
            }
            set => _timestamp = value;
        }
        /// <summary>
        /// 生成时间戳 
        /// </summary>
        /// <returns>当前时间减去 1970-01-01 00.00.00 得到的毫秒数</returns>
        public long GetTimeStamp()
        {
            DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
            DateTime nowTime = DateTime.Now;
            long unixTime = (long)System.Math.Round((nowTime - startTime).TotalMilliseconds, MidpointRounding.AwayFromZero);
            return unixTime;
        }

    }
}
